<template>
  <div class="home-header">
    <div class="content">
      <div class="left">
        <span @click="setHome">设为首页</span>
        <span @click="AddFavorite">收藏招标采购网</span>
        <span @click="goCenter">会员中心</span>
      </div>
      <div class="right">
        <img src="../assets/phone.png" alt="" class="phone" />
        <img src="../assets/head_logo.png" alt="" class="head_logo" />
        <template v-if="login">
          <span class="login" @click="goCenter">{{ username }}</span>
          <span @click="logout">退出登录</span>
        </template>
        <template v-else>
          <span class="login" @click="gologin">会员登录</span>
          <span @click="goreg">免费注册</span>
        </template>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
export default {
  computed: {
    ...mapState(["login", "userInfo"]),
  },
  data() {
    return {
      username: "",
    };
  },
  mounted() {
    // let data = JSON.parse(this.userInfo);
    let data = this.userInfo;
    this.username = data.username;
  },
  methods: {
    gologin() {
      this.$router.push("/login");
    },
    goreg() {
      this.$router.push("/register");
    },
    goCenter() {
      if (!this.login) {
        return;
      }
      this.$router.push("/personCenter/service_hall");
    },
    logout() {
      this.$store.commit("resetState");
    },
    setHome() {
        alert("抱歉，您所使用的浏览器无法完成此操作。\n\n您需要手动设置为首页。");
    },
    AddFavorite() {
      let sURL= window.location.href;
      let sTitle= '中国招标网';
      try{
        window.external.addFavorite(sURL, sTitle);
      }
      catch (e){
        try{
          window.sidebar.addPanel(sTitle, sURL, "")
        }
        catch (e){
          alert("加入收藏失败，请使用Ctrl+D进行添加")
        }
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.home-header {
  height: 30px;
  background: #cfcfcf;
  font-size: 12px;
  color: #2d2d2d;
  .content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 1000px;
    margin: auto;
  }
  .left span {
    cursor: pointer;
    margin-right: 10px;
  }
  .right {
    display: flex;
    align-items: center;
  }
  .head_logo {
    width: 60px;
  }
  .phone {
    width: 121px;
    margin-right: 19px;
  }
  .right span {
    cursor: pointer;
  }
  .login {
    display: inline-block;
    line-height: 0.8;
    border-right: 1px solid #5c5c5c;
    padding-right: 9px;
    margin: 0 9px;
  }
}
</style>